package com.lee.blog.controller;

import com.lee.blog.bean.Article;
import com.lee.blog.bean.Time;
import com.lee.blog.service.ArticleService;
import com.lee.blog.service.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class AdminController {

    @Autowired
    ArticleService articleService;

    @Autowired
    TimeService timeService;

    //进入后台首页
    @RequestMapping("/admin")
    public String admin(Model model){
        List<Article> allArticle = articleService.getAllArticle();
        model.addAttribute("allArticle",allArticle);
        return "admin";
    }

    //进入编写文章页面
    @RequestMapping("/write")
    public String write(){
        return "write";
    }

    //提交写好的文章
    @PostMapping("/doWrite")
    public String doWrite(Article article){
        articleService.writeArticle(article);
        return "redirect:/admin";
    }

    //进入修改文章页面
    @RequestMapping("/edit")
    public String edit(@RequestParam("id") Integer id, Model model){
        Article articleById = articleService.getArticleById(id);
        model.addAttribute("articleById", articleById);
        return "edit";
    }

    //提交修改好的页面
    @PostMapping("/doEdit")
    public String doEdit(Article article){
        articleService.doEdit(article);
        return "redirect:/admin";
    }

    //进入记录时间页面
    @RequestMapping("/writeTime")
    public String writeTime(){
        return "writeTime";
    }

    //提交记录好的时间
    @RequestMapping("/doWriteTime")
    public String doWriteTime(Time time){
        timeService.doWriteTime(time);
        return "redirect:/admin";
    }

    //删除文章
    @RequestMapping("/delete")
    public String delete(@RequestParam("id") Integer id){
        articleService.deleteArticleById(id);
        return "redirect:/admin";
    }

}
